const app = getApp()
let saving = false

Page({
  data: {
    name: '',
    gender:'',
    birthday: '',
    relation: '',
    infoId: '',
    openid: '',
    code: '',
    icode: '',
    iname: '',
    b2a: '',
    num: '',
  },
onLoad(x) {
  // console.log('onLoad =====', x)
  const i = app.globalData.info
 this.setData({
    name: i.members[x.co].name,
    gender: i.members[x.co].gender,
    birthday: i.members[x.co].birthday,
    relation: i.relations[x.code].a2b,
    infoId: i._id,
    openid: i._openid,
    num: x.code,
    code: x.co,
    icode: x.c,
    b2a: i.relations[x.code].b2a,
    iname: i.members[x.c] ? i.members[x.c].name : i.mine.name
  })
  console.log(this.data)
},
  onReady: function() {
    // 标题栏
    if (this.data.code) {
      wx.setNavigationBarTitle({
        title: '酒肆 - ' + this.data.name
      })
    }
  },
  xingming(x){
    this.setData( {
      name: x.detail.value
    })
  },
  xingbie(x){
    this.setData( {
      gender: x.detail.value
    })
  },
  nianling(x){
    this.setData({
      birthday: x.detail.value
    })
  },
  guanxi(x){
    let b2a =  x.detail.value
    if (b2a === '父母'){
      b2a = '子女'
    }else if (b2a === '子女'){
      b2a = '父母'
    }
    this.setData( {
      relation: x.detail.value,
      b2a: b2a,
    })
  },
  update(){
    if (saving){
      return
    }
    saving = true
    if (this.data.name === '' || this.data.relation === '') {
      // 提示框
      wx.showToast({
        icon: 'none',
        title: '关系和姓名不能为空',
      })
      saving = false
      return
    }
    const db = wx.cloud.database()
    db.collection('information').doc(this.data.infoId).update({
      data: {
        members: {
          [this.data.code]:{
            name: this.data.name,
            gender: this.data.gender,
            birthday: this.data.birthday,
          }
        },
        relations: {
          [this.data.num]:{
            a: this.data.icode,
            a2b: this.data.relation,
            b2a: this.data.b2a,
          }
        }
      },
    })
    wx.showToast({
      title: '修改成员信息成功',
    })
    saving = false
  },
})


// console.log("拿到的编码====", app.genertCode());